home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / DIKUMUD.ZIP / HEAP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-30  |  2.7 KB  |  137 lines

  1. /*
  2.   SillyMUD Distribution V1.1b             (c) 1993 SillyMUD Developement
  3.  
  4.   See license.doc for distribution terms.   SillyMUD is based on DIKUMUD
  5. */
  6.  
  7. #include <stdio.h>
  8. #define TRUE 1
  9. #define FALSE 0
  10.  
  11. #include "protos.h"
  12.  
  13. void SmartStrCpy(char *s1, const char *s2) /* ignore trailing spaces and \n */
  14. {
  15.    int i;
  16.  
  17.     i = strlen(s2);
  18.     while (s2[i] <= ' ')
  19.       i--;
  20.  
  21.     /* null terminate s1 */
  22.     s1[i+1]='\0';
  23.  
  24.     while (i>=0) {
  25.       s1[i] = s2[i];
  26.       i--;
  27.     }
  28.  
  29. }
  30.  
  31. void StringHeap(char *string, struct StrHeap *Heap)
  32. {
  33.    unsigned char found=FALSE;
  34.    int i;
  35.  
  36.    if (!string || !*string) {
  37.      return;   /* don't bother adding if null string */
  38.    }
  39.  
  40.    for (i=0;i<Heap->uniq&&!found;i++) {
  41.      if (!strcmp(string, Heap->str[i].string)) {
  42.        Heap->str[i].total++;
  43.        found=TRUE;
  44.      }
  45.    }
  46.    if (!found) {
  47.       if (Heap->str) {
  48.          /* increase size by 1 */
  49.          Heap->str = (struct StrHeapList *)
  50.        realloc(Heap->str, sizeof(struct StrHeapList)*Heap->uniq+1);  
  51.       } else {
  52.     Heap->str = (struct StrHeapList *)malloc(sizeof(struct StrHeapList));
  53.       } 
  54.       Heap->str[Heap->uniq].string = (char *)malloc(strlen(string)+1);
  55.       SmartStrCpy(Heap->str[Heap->uniq].string, string);
  56.       Heap->str[Heap->uniq].total=1;
  57.       Heap->uniq++;
  58.    }
  59.  
  60. }
  61.  
  62. struct StrHeap *InitHeap()
  63. {
  64.    struct StrHeap *Heap=0;
  65.  
  66.    Heap = (struct StrHeap *)malloc(sizeof(struct StrHeap));
  67.    Heap->str=0;
  68. /*
  69.    Heap->str = (struct StrHeapList *)malloc(sizeof(struct StrHeapList));
  70.    Heap->str[0].string=0;
  71.    Heap->str[0].total=0;
  72. */
  73.    Heap->uniq=0;
  74.    return(Heap);
  75. }
  76.  
  77. void DisplayStringHeap
  78.   (struct StrHeap *Heap, struct char_data *ch, int type, int destroy)
  79. {
  80.    char buf[256];
  81.    int i;
  82.  
  83.    for (i=0; i<Heap->uniq; i++) {
  84.      if (type != TO_CHAR) {
  85.         if (Heap->str[i].total > 1) {
  86.           sprintf(buf, "%s [%d]", Heap->str[i].string, Heap->str[i].total);
  87.         } else {
  88.            sprintf(buf, "%s", Heap->str[i].string);
  89.         }
  90.       } else {
  91.         if (Heap->str[i].total > 1) {
  92.            sprintf(buf, "%s [%d]\n\r", Heap->str[i].string, Heap->str[i].total);
  93.         } else {
  94.            sprintf(buf, "%s\n\r", Heap->str[i].string);
  95.         }
  96.       }
  97.      if (type == TO_CHAR) {
  98.        send_to_char(buf, ch);
  99.      } else {
  100.        if (ch->in_room > -1) {
  101.      act(buf, FALSE, ch, 0, 0, TO_ROOM);
  102.        }
  103.      }
  104.  
  105.      if (destroy) {
  106.         /* free everything */     
  107.         free(Heap->str[i].string);
  108.      }
  109.    }
  110.    if (destroy) {
  111.       free(Heap->str);
  112.       free(Heap);
  113.    }
  114. }
  115.  
  116. #if 0
  117. main()
  118. {
  119.    struct StrHeap *H;
  120.    int i;
  121.    char buf[256];
  122.  
  123.    H = InitHeap();
  124.  
  125.    for (i=1;i<10;i++) {
  126.       scanf("%s", buf);
  127.       StringHeap(buf, H);
  128.    }   
  129.  
  130.    DisplayStringHeap(H,0,TRUE);
  131.  
  132. }
  133. #endif
  134.  
  135.  
  136.  
  137.